﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.IO;
using iOSRemoteConnector;
using Microsoft.Surface.Presentation.Controls;
using System.Collections.ObjectModel;
using MediaSurfaceV2.MediaConsumerServiceReference;
using System.Collections;


namespace MediaSurfaceV2
{
    /// <summary>
    /// Interaction logic for DraggedItemControl.xaml
    /// </summary>
    public partial class DraggedItemControl : SurfaceUserControl
    {
        bool playingState;
        private ObservableCollection<mediaConsumerListing> mediaLibraryList2;

        public DraggedItemControl()
        {
            InitializeComponent();
        }

        public DraggedItemControl(MemoryStream videoSource)
        {
            InitializeComponent();

            // Video will play on load.
            videoElement.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
            videoElement.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;

            // The media dimensions are not available until the MediaOpened event.
            videoElement.MediaOpened += delegate
            {
                // Size the ScatterViewItem control according to the video size.
                videoElement.Height = videoElement.NaturalVideoHeight;
                videoElement.Width = videoElement.NaturalVideoWidth;
            };

            Random a = new Random();
            Double myDouble = a.NextDouble();


            FileStream fs = File.OpenWrite(myDouble.ToString() + ".mp4");
            videoSource.WriteTo(fs);
            fs.Close();

            videoElement.MediaFailed += delegate
            {
                this.Content = "Unable to play the video";
            };

            // Load and play the video.
            videoElement.Source = new Uri(@"C:\Users\admin\Desktop\MediaSurfaceV2\MediaSurfaceV2\bin\Debug\" + myDouble.ToString() + ".mp4");
            videoElement.Play();
            playButton.Source = new BitmapImage(new Uri(@"C:\Users\Public\coquette-part-6-icons-set\png\128x128px\pause.png"));
            playingState = true;
        }

        private void ServerClick(object sender, RoutedEventArgs e)
        {
           string result = UploadToMediaServer(videoElement.Source.AbsolutePath);

           Console.Out.WriteLine(result);
        }


        public void AddMediaToLibrary() //Intialize the library with all the media file
        {
            MediaConsumerClient client = new MediaConsumerClient();


            IList resultList = client.listMediaInPool(client.getMediaPools());

            if (resultList != null)
            {
                foreach (mediaConsumerListing item in resultList)
                {

                    mediaLibraryList2.Add(item);

                }

                


            }

            else { }

        }

        private void TVClick(object sender, RoutedEventArgs e)
        {
            //DUMMY FUNCTION TODO LATER
        }

        private void iPhoneClick(object sender, RoutedEventArgs e)
        {

            SurfaceBridge sb = new SurfaceBridge();

            sb.PlayMovieOniPad("Software Engineering Failz"); 



            
        }

        public string UploadToMediaServer(String mediaName)
        {
            string UploadSuccess = "upload is not successful";
            string s = mediaName;
            MediaManagerServiceReference.MediaManagerClient up = new MediaManagerServiceReference.MediaManagerClient();

            byte[] rawData = File.ReadAllBytes(s);

            string[] tags = { "family", "funny" };

            try
            {

                up.fileUpload(System.IO.Path.GetFileName(mediaName), rawData, "bill", tags, "Baby's first steps", "This baby looks so cute!!!!");
                UploadSuccess = "upload is succesful";
            }
            catch { }


            rawData = null;
            GC.Collect();

            return UploadSuccess;

        }

        private void RepeatMedia(object sender, ContactEventArgs e)
        {
            videoElement.Stop();
            videoElement.Play();
        }

        private void StopMedia(object sender, ContactEventArgs e)
        {
            videoElement.Stop();
            playingState = false;
            playButton.Source = new BitmapImage(new Uri(@"C:\Users\Public\coquette-part-6-icons-set\png\128x128px\play.png"));
        }

        private void PlayMedia(object sender, ContactEventArgs e)
        {
            if (playingState == true)
            {
                videoElement.Pause();
                playingState = false;
                playButton.Source = new BitmapImage(new Uri(@"C:\Users\Public\coquette-part-6-icons-set\png\128x128px\play.png"));

            }
            else if (playingState == false)
            {
                videoElement.Play();
                playingState = true;
                playButton.Source = new BitmapImage(new Uri(@"C:\Users\Public\coquette-part-6-icons-set\png\128x128px\pause.png"));
            }
        }
		
		private void CloseMedia(object sender, ContactEventArgs e)
		{
			videoElement.Stop();
			DependencyObject parent = LogicalTreeHelper.GetParent((Image)sender);
            while (parent != null)
            {
                ScatterViewItem item = parent as ScatterViewItem;
                if (item != null)
                {
                    ScatterView scatterView = LogicalTreeHelper.GetParent(item) as ScatterView;
                    if (scatterView != null)
                    {
                        scatterView.Items.Remove(item);
                        return;
                    }
                }

                parent = LogicalTreeHelper.GetParent(parent);
            }	
		}

     
    }
}
